import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, FormInstance } from "element-plus";
import { useRoute } from "vue-router";
import { ref, reactive, onMounted } from "vue";
import { ProCustomGetProCustom } from "@/api/stu/InsuranceData";
import { emitter } from "@/utils/mitt";

export function usePDataIntegration() {
  const route = useRoute();
  const Visible = ref(false);
  const loading = ref(false);
  const dataList = ref();
  const MySignIn = ref("");
  const SetMyJSonar = ref(); // 保存子组件内容
  const RefScreenConformity = ref();
  const SelectCustomerBase = ref();
  const RefCustomerGroup = ref();
  const CustomerGroup = reactive({
    name: ""
  });
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 30,
    currentPage: 1,
    pageSizes: [5, 10, 30],
    background: true
  });

  const columns = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    },
    {
      label: "姓名",
      prop: "name",
    }, {
      label: "性别",
      prop: "sex",
    }, {
      label: "年龄",
      prop: "age",
    }, {
      label: "民族",
      prop: "nation",
    }, {
      label: "身份证号",
      prop: "idCard",
    }, {
      label: "电话号码",
      prop: "phone",
    }, {
      label: "婚姻状况",
      prop: "marriageState",
    }, {
      label: "省份",
      prop: "province",
    },
  ];

  // 根据已保存客户群去搜索
  async function SelectDataById() {
    console.log("根据已保存客户群去搜索");
    // emitter.emit("ViewSecurities", res.data.conditionData)
  }

  function handleSizeChange(val) {
    getListData(SetMyJSonar.value);
  }

  function handleCurrentChange(val) {
    getListData(SetMyJSonar.value);
  }

  function HideVisible(row: FormInstance | undefined) {
    row.resetFields();
    Visible.value = false;
  }

  // 创建客户群 -- 窗口
  emitter.on('EM_openVisible', async (row) => {
    SetMyJSonar.value = eval(row.value);
    Visible.value = true;
  });

  // 创建客户群
  function getCustomerGroup(RefCustomerGroup: FormInstance | undefined) {
    (RefCustomerGroup as any).validate(async (valid) => {
      if (!valid) return;
      console.log("RefCustomerGroup", RefCustomerGroup);
    });
  }

  // 清空选择的客户群
  emitter.on("clearChoose", () => {
    MySignIn.value = "";
    SetMyJSonar.value = "";
    getListData(SetMyJSonar.value);
  });

  emitter.on("EmitCustomerGroup", async (row) => {
    getListData(row);
  });

  async function getListData(row) {
    // 获取客户群列表
    SetMyJSonar.value = row;
    const data = {
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      planId: route.query.planId,
      taskId: route.query.taskId,
      name: CustomerGroup.name,
      arr: row || []
    };
    const res = await ProCustomGetProCustom(data);
    if (res.success) {
      dataList.value = res.data.items;
      pagination.total = res.data.totalCount;
    } else {
      ElMessage.error(res.msg);
    }
  }

  onMounted(() => {
    getListData(undefined);
  });

  return {
    columns,
    loading,
    Visible,
    dataList,
    MySignIn,
    pagination,
    CustomerGroup,
    RefCustomerGroup,
    SelectCustomerBase,
    RefScreenConformity,
    HideVisible,
    SelectDataById,
    getCustomerGroup,
    handleSizeChange,
    handleCurrentChange
  };
}
